import { _decorator, Component, find, Node, Rect, tween, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MonsterAmmo')
export class MonsterAmmo extends Component {
    private myTween: any;

    private cloneMonsterNode: any;


    start() {
        globalThis.monsterAmmo = this;
        this.cloneMonsterNode = this.node.parent;
    }

    _init() {
        const myNode = this.node;
        const newPosition = new Vec3(0, 0, 0);

        // 使用 setPosition 方法设置节点的位置
        myNode.setPosition(newPosition);

        // 递归函数用于执行 Tween 动画
        const playTween = () => {
            this.node.active = true;
            

            var Player = find('Canvas/player');
            var monsterx = this.node.parent.position.x;
            var monstery = this.node.parent.position.y;

            console.log('player',Player);
            
            var playerx = Player.position.x;
            var playery = Player.position.y;
            var playerJulix = monsterx - playerx;
            var playerJuliy = monstery - playery;
            const myNode = this.node;
            const newPosition = new Vec3(0, 0, 0);

            // 使用 setPosition 方法设置节点的位置
            myNode.setPosition(newPosition);

            this.myTween = tween(this.node)
                .by(2.0, { position: new Vec3(-playerJulix, -playerJuliy, 0) })
                .call(() => {
                    // 继续播放 Tween 动画
                    playTween();
                })

                .start();


        };
        // 开始第一次播放 Tween 动画
        playTween();


    }
    


}

